package de.rac.vaadin.framework.ui;
/*******************************************************************************
 * Data for an edit form field that is a list of constant values<br>
 * @author Volodymyr Dobryvechir (dobrivecher@yahoo.com)
 * @version 0.2 31.05.2012  <br>
 ******************************************************************************/
import java.util.*;

import com.vaadin.ui.Field;
import com.vaadin.ui.ComboBox;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property;
import com.vditl.core.DataOrgano;
public class ListEditFormField extends EditFormField {
  
	private static final long serialVersionUID = 1L;
	public static final int OPTION_STARTWITHONE=1;
	public static final int OPTION_PLACEFIRSTASLAST=2;
	public static final int OPTION_MAYNOTBEINDEXED=4;
	protected LinkedHashMap<Object,String> listMap,showListMap;
	protected Class<?> keyType;
	protected ComboBox combo;
	public ListEditFormField(String colName,String caption,boolean required,Class<?> keyType,ResourcePool resource)
    {
    	super(colName,caption,required,resource);
    	this.keyType = keyType;
    }
	public ListEditFormField(String colName,String caption,boolean required,Class<?> keyType,String listing,ResourcePool resource)
    {
		this(colName,caption,required,keyType,resource);
    	initList(listing,resource,0);
    }
	public ListEditFormField(String colName,String caption,boolean required,Class<?> keyType,String listing,ResourcePool resource,int options)
    {
		this(colName,caption,required,keyType,resource);
    	initList(listing,resource,options);
    }
	public LinkedHashMap<Object,String> getListMap() { return listMap; }
	public void initList(String listing,ResourcePool resource,int options)
	{
         listMap = new LinkedHashMap<Object,String>();
		 int n= DataOrgano.GetSmartFieldQuantity(listing, ',');
		 int base=(options & OPTION_STARTWITHONE)==0?0:1;
		 boolean placeFirstAsLast=(options & OPTION_PLACEFIRSTASLAST)!=0;
		 Object ofirst=null;String sfirst=null;
         for(int i=0;i<n;i++)
         {
        	 String listItem= DataOrgano.GetSmartFieldValue(listing, ',', i);
        	 if (listItem.length()==0) continue;
        	 String key = listItem;
        	 String val = listItem;
        	 int p=listItem.indexOf("->");
        	 boolean mayBeIndexed=(options & OPTION_MAYNOTBEINDEXED)==0;
        	 if (p>=0)
        	 {
        		 key=listItem.substring(0, p).trim();
        		 val=listItem.substring(p+2).trim();
        		 mayBeIndexed=false;
        	 }
        	 Object okey = key;
        	 if (keyType==Integer.class)
        	 {
        		 if (mayBeIndexed) okey=new Integer(i+base);
        		 else
        		 okey=new Integer(Integer.parseInt(key));
        	 } else if (keyType==Long.class)
        	 {
        		 if (mayBeIndexed) okey=new Long(i+base);
        		 else
        		 okey=new Long(Long.parseLong(key));
        	 }
        	 if (i==0 && placeFirstAsLast)
        	 {
        		 ofirst=okey;sfirst=val;
        	 }
        	 else
        	 listMap.put(okey, val);
         }
         if (placeFirstAsLast)
         {
        	 if (ofirst!=null || sfirst!=null) listMap.put(ofirst,sfirst);
         }
         createFinalListMap(resource);
	}
	public void createFinalListMap(ResourcePool resource)
	{
		showListMap = new LinkedHashMap<Object,String>();
		for(Object item:listMap.keySet())
		{
			String val = listMap.get(item);
			String softval = resource.getTranslationSoft(val);
			if (softval!=null) val = softval;
			showListMap.put(item, val);
		}
	}
	@Override
    public void notifyOfLocaleChange(ResourcePool resource,boolean activeEdit)
    {
    	super.notifyOfLocaleChange(resource, false);
    	createFinalListMap(resource);
    	if (combo!=null)
    	{
    		Object key = combo.getValue();
    		combo.setValue(null);
    		combo.removeAllItems();
    		for(Object tp:showListMap.keySet())
    		{
   		        combo.addItem(tp);
    			combo.setItemCaption(tp, showListMap.get(tp));
    		}
    		combo.setCaption(caption);
    		String val = key==null?null:(String)showListMap.get(key);
    		combo.setData(val);
    		combo.setValue(key);
   	    }
    }

	@Override
    public Field getFieldGenerated(Item item) 
    {
		super.getFieldGenerated(item);
       if (combo==null)
       {
    	   combo = new ComboBox(caption);
   		   for(Object tp:showListMap.keySet())
   		   {
   		       combo.addItem(tp);
   		       combo.setItemCaption(tp, showListMap.get(tp));
   		   }
   		   combo.setNewItemsAllowed(false);
   		   combo.setNullSelectionAllowed(!required);
   		   combo.setRequired(required);
   		   combo.setVisible(true);
   		   combo.setImmediate(true);
   		   combo.setWriteThrough(true);
   	       if (subDependField.size()>0)
   	       {
   	    	   combo.addListener(new Property.ValueChangeListener() {
   	    		private static final long serialVersionUID = 1L;

				@Override
				public void valueChange(ValueChangeEvent event) {
					//System.out.println("combo change value:"+combo.getValue());
					checkVisibilityDependenciesForCurrentItem();
				}
			});
  	   
   	       }
       } 
       Object res=null;
       Object val = item.getItemProperty(columnName).getValue();
       if (val!=null) res=listMap.get(val);
       if (res==null) combo.setValue(null);
       else combo.setValue(val);
       setCurrentField(combo);
       return combo;
    }

}
